package specifications;

import java.lang.reflect.Field;

import entities.Country;

public class CountrySpecification implements Specification<Country> {
	private String attribute;
    private Object value;

    public CountrySpecification(String attribute, Object value) {
        this.attribute = attribute;
        this.value = value;
    }

    @Override
    public final boolean isSatisfiedBy(Country candidate) {
        Field field = null;
		try {
			field = candidate.getClass().getDeclaredField(this.getAttribute());
		} catch (NoSuchFieldException | SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        Object candidateValue = null;
		try {
			field.setAccessible(true);
			candidateValue = field.get(candidate);
		} catch (IllegalArgumentException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return isSatisfiedByValue(candidateValue);
    }

    protected boolean isSatisfiedByValue(Object candidateValue) {
		return candidateValue.equals(getValue());
    }

    public String getAttribute() {
        return attribute;
    }

    public Object getValue() {
        return value;
    }
}
